package com.colin.app.activity.main;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.RadioGroup;

import com.colin.app.R;
import com.colin.app.app.AppActivity;
import com.colin.app.floatview.FloatWindowManager;
import com.colin.app.floatview.FloatWindowService;
import com.colin.app.floatview.FloatWindowSmallView;
import com.colin.library.adapter.MyFragmentPagerAdapter;
import com.colin.library.base.BaseFragment;
import com.colin.library.listener.MyAddOnPageChangeListener;

import java.util.List;

public class MainActivity extends AppActivity implements BaseFragment.OnFragmentListener, MainContract.MainView {
    private MainContract.MainPresenter mMainPresenter = null;
    private ViewPager pager_main_content;
    private RadioGroup radiogroup_main_menu;
    private MyFragmentPagerAdapter adapter = null;


    @Override
    protected void onDestroy() {
        adapter = null;
        mMainPresenter.destroy();
        mMainPresenter = null;
        super.onDestroy();
    }

    @Override
    protected boolean isHaveBack() {
        return false;
    }

    @Override
    public void initPresenter() {
        mMainPresenter = new MainPresenterImpl(this, this);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        this.pager_main_content = (ViewPager) this.findViewById(R.id.pager_main_content);
        this.radiogroup_main_menu = (RadioGroup) this.findViewById(R.id.radiogroup_main_menu);
    }

    @Override
    public void initData() {
        mMainPresenter.setData();
    }

    @Override
    public void initViewPager(List<Integer> fragmentIDList, List<Fragment> fragmentList) {
        if (adapter == null) {
            adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList, fragmentIDList);
        }
        pager_main_content.setAdapter(adapter);
        pager_main_content.setOffscreenPageLimit(fragmentList.size());

    }

    @Override
    public void initListener() {
        pager_main_content.addOnPageChangeListener(new MyAddOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                mMainPresenter.select(position, pager_main_content, radiogroup_main_menu);
            }
        });

        radiogroup_main_menu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int position = 0;
                switch (checkedId) {
                    case R.id.radio_table_home:
                        position = 0;
                        break;
                    case R.id.radio_table_demo:
                        position = 1;
                        break;
                    case R.id.radio_table_permission:
                        position = 2;
                        break;
                    case R.id.radio_table_mine:
                        position = 3;
                        break;
                }
                mMainPresenter.select(position, pager_main_content, radiogroup_main_menu);
            }
        });
    }



    @Override
    public void onFragmentClick(int fragment_id, boolean isOK, int position, Object object) {

    }

}
